﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Prsrp
{
    /// <summary>
    /// This class represent an animated sprite.
    /// </summary>
    /// <remarks>
    /// Call Draw() to draw it on the game screen.
    /// </remarks>
    public class Animation
    {
        public Texture2D mpTexture;
        private float mfFrameRate = 1000f / 10f;
        private int miNbOfFrames;
        public int miCurFrame = 0;
        private float mfTimer = 0f;
        private Vector2 mvSize;
        public Vector2 mvOffset;
        public bool mbLooping = true;
        public bool mbReachEnd = false;

        /// <summary>
        /// The width of the animation.
        /// </summary>
        public int Width
        {
            get { return (int)mvSize.X; }
            set { mvSize.X = value; }
        }

        /// <summary>
        /// The height of the animation.
        /// </summary>
        public int Height
        {
            get { return (int)mvSize.Y; }
            set { mvSize.Y = value; }
        }

        /// <summary>
        /// Default constructor from a <typeparamref name="Texture2D"/> argument.
        /// </summary>
        /// <param name="_mpTexture">The Texture2D handler.</param>
        /// <param name="_iNbOfFrames">The number of frames in the animation.</param>
        public Animation(Texture2D _mpTexture, int _iNbOfFrames)
        {
            mpTexture = _mpTexture;
            miNbOfFrames = _iNbOfFrames;
            mvSize = new Vector2(mpTexture.Width/miNbOfFrames, mpTexture.Height);
        }

        public void Draw(Vector2 _pPos, float _fScale, bool _bFlip)
        {
            // todo: find a clearer way to do this...
            if (!Game.Engine.mbDisableUpdate)
            {
                // Update
                float fDeltaTime = (float)Game.Engine.GameTime.ElapsedGameTime.TotalMilliseconds;
                mfTimer += fDeltaTime;
                if (mfTimer > mfFrameRate)
                {
                    miCurFrame++;
                    if (miCurFrame > miNbOfFrames - 1)
                    {
                        if (mbLooping)
                        {
                            miCurFrame = 0;
                        }
                        else
                        {
                            mbReachEnd = true;
                        }
                    }
                    mfTimer = 0f;
                }
            }

            Vector2 pDrawPos = _pPos - mvOffset;
            Rectangle pCropRectangle = new Rectangle(miCurFrame * (int)mvSize.X, 0, (int)mvSize.X, (int)mvSize.Y);
            SpriteEffects pCurSpriteEffect = _bFlip ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
            Game.Engine.SpriteBatch.Draw(mpTexture, pDrawPos, pCropRectangle, Color.White, 0f, new Vector2(0, mvSize.Y / _fScale), _fScale, pCurSpriteEffect, 0);
        }
    }
}
